#include "common.h"
#include <stdio.h>

extern int ADXL345_DataX;
extern int ADXL345_DataY;
extern int ADXL345_DataZ;

extern void ADXL345_Read();

static unsigned int acc_data_last[5];
static unsigned int acc_buff[6];
static uint32_t new_acc_data_ft;
static uint32_t acc_data_ret;
static uint32_t acc_data_avarage;

static uint32_t last_value;


int ABS(int a, int b)
{
    if (a>=b)
      return a-b;
    else
      return b-a;
}

int motion_know(int mode, int clearmark)
{
    static uint32_t bigmotion = 0;
    static uint32_t count = 0;
    static uint8_t start_flag = 0;
    static uint8_t ok_flag = 0;
    static uint8_t allow = 1;
    static uint32_t cold_mode = 0;

    int tmp_value = (int)(ADXL345_DataX + ADXL345_DataY + ADXL345_DataZ);
    uint32_t new_acc = 0;
    
    if (tmp_value>=0)
        new_acc = tmp_value;
    else
        new_acc = -tmp_value;
  
    acc_buff[4] = acc_buff[3];
    acc_buff[3] = acc_buff[2];
    acc_buff[2] = acc_buff[1];
    acc_buff[1] = acc_buff[0];
    acc_buff[0] = new_acc;    
    
    new_acc_data_ft = (acc_buff[0] + acc_buff[1] + acc_buff[2] + acc_buff[3] + acc_buff[4]) / 5 ;
     
    acc_buff[4] = acc_buff[3];
    acc_buff[3] = acc_buff[2];  
    acc_buff[2] = acc_buff[1];
    acc_buff[1] = acc_buff[0];
    acc_buff[0] = new_acc_data_ft;

    acc_data_avarage = 0;
    for(int i=0;i<5;i++)
    {
        acc_data_avarage += acc_buff[i];
    }
    acc_data_ret = acc_data_avarage / 5;
    
    if (clearmark == 1)
    {     
        last_value = acc_data_ret;
        return 80;
    }
    
    if (ABS(last_value, acc_data_ret) >= 80)
    {
        count ++;
        if (count == 4)
        {
            last_value = acc_data_ret;
            count = 0;
            start_flag ++;
        }
        bigmotion = 0;
        cold_mode = 0;
     }
     else
     {
        count = 0;
        if (cold_mode ++ >= 28)
        {
            cold_mode = 80;
            printf("cold mode\r\n");

            if (allow == 0)
            {
                allow = 1;
                start_flag = 0;
                set_event(RTC_TASK, EVENT_RTC_RECORD_END);
                //printf("motion end\r\n");
            }    
          }
     }
   
     if (start_flag>= 3 && allow)
     { 
         //printf("googogogogogogoogogogogogo\r\n");
         set_event(RTC_TASK, EVENT_RTC_RECORD);
         allow = 0;
     }
     
     bigmotion ++;
     if (bigmotion <= 600)  // 80 * 750 = 60S ,work with low mode
     {
        return 50;
     }
     else
     {
        bigmotion = 400;
        return 1000;
     }
}


#include "lis3dh_driver.h"

uint8_t position=0, old_position=0;
uint8_t response;
uint32_t WaterGlass()
{
 
    
    
    static int flag = 0;
    
    if (!flag )
    {
      flag = 1;
      response = LIS3DH_SetODR(LIS3DH_ODR_50Hz);

      //set PowerMode 
      response = LIS3DH_SetMode(LIS3DH_NORMAL);

      //set Fullscale
      response = LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);

      //set axis Enable
      response = LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);

      //set Interrupt Threshold 
      response = LIS3DH_SetInt1Threshold(40);

      //set Interrupt configuration (all enabled)
      response = LIS3DH_SetIntConfiguration(LIS3DH_INT1_ZHIE_ENABLE | LIS3DH_INT1_ZLIE_ENABLE |
                                                                               LIS3DH_INT1_YHIE_ENABLE | LIS3DH_INT1_YLIE_ENABLE |
                                                                               LIS3DH_INT1_XHIE_ENABLE | LIS3DH_INT1_XLIE_ENABLE ); 

      //set Interrupt Mode
      response = LIS3DH_SetIntMode(LIS3DH_INT_MODE_6D_POSITION);
      
      response = LIS3DH_SetInt1Duration(0xA);
    }
    
    //get 6D Position
    response = LIS3DH_Get6DPosition(&position);
        
    //if((response==1) && (old_position!=position))
    switch (position)
    {
                case LIS3DH_UP_SX:   printf("position = UP_SX \r\n");   
                break;
                case LIS3DH_UP_DX:   printf("position = UP_DX  \r\n");   
                break;
                case LIS3DH_DW_SX:   printf("position = DW_SX  \r\n");  
                break;              
                case LIS3DH_DW_DX:   printf("position = DW_DX  \r\n");  
                break; 
                case LIS3DH_TOP:      printf("position = TOP   \r\n"); 
                break; 
                case LIS3DH_BOTTOM:  printf("position = BOTTOM \r\n");  
                break; 
                default:     // sprintf((char*)buffer,"\n\rposition = unknown\n\r\0");  
                      printf("00000\r\n");
                      break;
    }
    
    //function for MKI109V1 board   
    old_position = position;
}


void Doorkeeper()
{
    
  
}


